package upmc.api.aladyn.server.main;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.text.ParseException;
import java.util.Date;

import javassist.*;
import javassist.CtField.Initializer;

import upmc.api.aladyn.server.services.*;

/** Main Class of server, it implements the Runnable
 * 
 * @author Z_WANG
 *
 */
public class Server implements Runnable {
	/**
	 * Socket client
	 */
	private Socket client;
	/**
	 * ServerSocket server
	 */
	private ServerSocket server;
	
	/**
	 * Method run used to listen the client, and get the record of client and send back the response
	 */
	public void run(){
        
        try {
           server=new ServerSocket(5678);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try{
            
            while(true){
                String returnStr = "";
                client = server.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                PrintWriter out = new PrintWriter(client.getOutputStream());
                
                String reciverInfo = in.readLine();
                
                returnStr = new CreateOrReadXMLMethods().ReadXMLAndGenerateReponse(reciverInfo);
                //your code
                out.println(returnStr);
                out.flush();    
                client.close();
            }
            
        } catch (IOException ex){
            ex.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            
        }

    }
	/**
	 * Constructor of the server
	 */
	public Server() {
		super();
	}



	/**Main method
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws ParseException 
	 */
	public static void main(String[] args) throws IOException, InterruptedException, ParseException{
		// here is to start the server
//		 Thread t = new Thread(new Server());
//	     t.start();
	     
	     
		String strInput = "";
		
//		ServerSocket server=new ServerSocket(5678);
//		Socket client=server.accept();   
//		BufferedReader in=  new BufferedReader(new InputStreamReader(client.getInputStream()));   
//		PrintWriter out=new PrintWriter(client.getOutputStream()); 
//		
//		strInput=in.readLine();   
//		//System.out.println(strInput);
//		
//		if(strInput != null && strInput != "")
//		{
//			
//			if(strInput.equals("Input"))
//			{
//				strInput = "String Output";
//			}
//			strInput = new CreateOrReadXMLMethods().ReadXMLToCreateClass(strInput);
//			String strOutput = strInput;
//			out.println(strOutput);
//            out.flush();
//		}
//		
//		
//		client.close();   
		
		strInput = "<methodCall>" +
						"<methodName>displayStringUseAnotherStringable</methodName>" +
						"<params>" +
							"<param>" +
								"<value>" +
									"<object  oid=\"http://www.jm.fr/1000000\" type=\"AnotherStringable\">" +
										"<fields>" +
											"<field name=\"x\"><value><double>100.0</double></value></field>" +
											"<field name=\"y\"><value><int>20</int></value></field>" +
											"<field name=\"mark\"><value><string>@</string></value></field>" +
											"<field name=\"recordTime\"><value><dateTime.iso8601>20051130T12:10:56</dateTime.iso8601></value></field>" +
											"<field name=\"testArray\"><value><dateTime.iso8601>20051130T12:10:56</dateTime.iso8601></value><value><string>@</string></value><value><double>56.2</double></value></field>" +
										"</fields>" +
										"<methods>" +
											"<method language=\"Java\">public String getPosition(){return this.x + this.mark + this.y;}</method>" +
										"</methods>" +
									"</object>" +
								"</value>" +
							"</param>" +
						"</params>" +
					"</methodCall>";
		
		String strInputWithObject = "<methodCall>" +
				"<methodName>displayStringUseStringable</methodName>" +
				"<params>" +
					"<param>" +
						"<value>" +
							"<object  oid=\"http://www.jm.fr/1000000\" type=\"Stringable\">" +
								"<fields>" +
									"<field name=\"x\"><value><double>100.0</double></value></field>" +
									"<field name=\"y\"><value><int>580</int></value></field>" +
									"<field name=\"mark\"><value><string>@</string></value></field>" +
									"<field name=\"point\"><value>" +
										"<object  oid=\"http://www.jm.fr/1000001\" type=\"Stringable\">" +
											"<fields>" +
												"<field name=\"px\"><value><double>800.0</double></value></field>" +
												"<field name=\"py\"><value><int>666</int></value></field>" +
												"<field name=\"pmark\"><value><string>@</string></value></field>" +
											"</fields>" +
											"<methods>" +
												"<method language=\"Java\">public String getPosition(){return this.px + this.pmark + this.py;}</method>" +
											"</methods>" +
										"</object>" +
									"</value></field>" +
								"</fields>" +
								"<methods>" +
									"<method language=\"Java\">public String getPosition(){return this.x + this.mark + this.y;}</method>" +
								"</methods>" +
							"</object>" +
						"</value>" +
					"</param>" +
				"</params>" +
			"</methodCall>";
		
		new CreateOrReadXMLMethods().ReadXMLAndGenerateReponse(strInput);
		Thread.sleep(2000);
		new CreateOrReadXMLMethods().ReadXMLAndGenerateReponse(strInputWithObject);
		
	} 

}
